<?php


namespace app\common\controller;


use app\common\model\OrderLog;

class DataWrite
{
    /** 原始数据写入 */
    public function rawDataWrite($data,$type)
    {
        $today = date('Ymd',time());
//        $root_dir = "D:\phpStudy\PHPTutorial\WWW/sange/log/raw/".$data['org_id']."/".$data['app_id']."/".$type."/";
        $root_dir = "/data/wwwroot/see/log/raw/".$data['org_id']."/".$data['app_id']."/".$type."/";
        //要创建的两个文件
        $fileName = $root_dir.$today.".log";

        $path = dirname($fileName);
        if(!is_dir($path)){
            mkdir($path,0777,true);
			chmod($path,0777);
        }

        // 压缩
        if($type != 'daily'){
            $yes = date('Ymd',time()-86400);
            $yes_path     = $root_dir.$yes.".log";
            $yes_zip     = $root_dir.$yes.".zip";
            if(file_exists($yes_path) && !file_exists($yes_zip)){
                $zip      = new \ZipArchive();
                $zip->open($yes_zip, \ZipArchive::CREATE);   //打开压缩包
                $zip->addFile($yes_path, basename($yes_path));   //向压缩包中添加文件
                $zip->close();  //关闭压缩包
                unlink($yes_path);
            }
        }

        //以读写方式打写指定文件，如果文件不存则创建
        if( ($TxtRes=fopen ($fileName,"a")) === FALSE){
            OrderLog::record('','创建可写文件失败',$fileName);
        }else{
            $StrConents = json_encode($data)."\r\n";
            if(!fwrite ($TxtRes,$StrConents)){ //将信息写入文件
                OrderLog::record('','尝试向文件失败',$fileName);
                fclose($TxtRes);
            }else{
                fclose ($TxtRes); //关闭指针
            }

        }

    }
}